﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BUS;

public partial class QuanLyNhanVien : System.Web.UI.Page
{
    DataTable dtNV;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");
        if (Boolean.Parse(Session["QuanLy"].ToString()) == false)
            Response.Redirect("../Default.aspx");
        if (!IsPostBack)
        {
            dtNV = NhanVienBUS.LayDanhSachNhanVien();
            GridView1.DataSource = dtNV;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = dtNV = NhanVienBUS.LayDanhSachNhanVien();
        GridView1.DataBind();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Nhan_Vien nv = new Nhan_Vien();
        GridViewRow row = this.GridView1.Rows[e.RowIndex];
        nv.TenNhanVien = ((TextBox)(row.Cells[0].FindControl("TextBox1"))).Text;
        nv.CMND = row.Cells[1].Text;
        nv.DiaChi = ((TextBox)row.Cells[2].FindControl("TextBox2")).Text;
        nv.DienThoai = ((TextBox)row.Cells[3].FindControl("TextBox3")).Text;
        nv.Email = ((TextBox)row.Cells[4].FindControl("TextBox4")).Text;
        nv.QuanLy = ((CheckBox)row.Cells[5].Controls[0]).Checked;
        nv.KhongHoatDong = ((CheckBox)row.Cells[6].Controls[0]).Checked;
        GridView1.EditIndex = -1;
        NhanVienBUS.SuaNhanVien(nv);
        GridView1.DataSource = dtNV = NhanVienBUS.LayDanhSachNhanVien();
        GridView1.DataBind();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = dtNV = NhanVienBUS.LayDanhSachNhanVien();
        GridView1.DataBind();
        gotoAnchorScript("list");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String cmnd = GridView1.Rows[e.RowIndex].Cells[1].Text;
        NhanVienBUS.XoaNhanVien(cmnd);
        GridView1.DataSource = dtNV = NhanVienBUS.LayDanhSachNhanVien();
        GridView1.DataBind();
        gotoAnchorScript("list");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = dtNV = NhanVienBUS.LayDanhSachNhanVien();
        GridView1.DataBind();
        gotoAnchorScript("list");
    }
    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        tblThem.Visible = true;
        btnThem.Visible = false;
        gotoAnchorScript("form");
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        Nhan_Vien nv = new Nhan_Vien();
        nv.TenNhanVien = txtTen.Text.Trim();
        nv.CMND = txtCMND.Text.Trim();
        nv.DiaChi = txtDiaChi.Text.Trim();
        nv.DienThoai = txtDienThoai.Text.Trim();
        nv.Email = txtEmail.Text.Trim();
        nv.QuanLy = chkQuanLy.Checked;
        nv.MatKhau = nv.CMND;
        nv.KhongHoatDong = false;
        nv.NgayVaoLam = DateTime.Now;
        NhanVienBUS.ThemNhanVien(nv);
        Response.Redirect("QuanLyNhanVien.aspx#list");
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLyNhanVien.aspx#list");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Int32 idx = Int32.Parse(e.CommandArgument.ToString());
        if (e.CommandName.Equals("Reset"))
        {
            String manv = GridView1.Rows[idx].Cells[1].Text;
            NhanVienBUS.ResetPassword(manv);
            Response.Write("<script \"text/javascript\">alert('Thay đổi thành công!');</script>");
            gotoAnchorScript("list");
        }
    }
}
